<template>
  <div class="home">
    <div class="top">
      <p>运维工单</p>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>资产管理</el-breadcrumb-item>
        <el-breadcrumb-item>设备运维</el-breadcrumb-item>
        <el-breadcrumb-item>运维工单</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="header">
      <!-- 搜索 -->
      <el-input
      clearable
        style="width: 150px"
        v-model="uintName"
        placeholder="工单名称"
      ></el-input>
      <el-input
      clearable
        style="width: 150px"
        v-model="exePerson"
        placeholder="执行人员"
      ></el-input>
      <el-select clearable style="width: 150px" v-model="status" placeholder="状态">
        <el-option v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-date-picker clearable v-model="startTime" align="right" type="date" placeholder="开始日期">
      </el-date-picker>
      <el-date-picker clearable v-model="endTime" align="right" type="date" placeholder="结束日期">
      </el-date-picker>
      <el-button
        @click="onSearch"
        style="background-color: #0bb197"
        type="success"
        icon="el-icon-search"
        >查询</el-button
      >
      <el-button class="reset" @click="onReset" icon="el-icon-refresh">重置</el-button>
    </div>

    <!-- 数据 -->
    <div class="body">
      <div class="div">
        <h4>查询结果列表</h4>
        <el-button
          style="background-color: #0bb197;border:1px solid #0bb197"
          type="primary"
          @click="addDialog = true"
          >新增</el-button
        >
      </div>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column
          align="center"
          prop="id"
          label="序号"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="ticketType"
          label="工单类型"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="ticketNum"
          label="工单编号"
          width="150"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="ticketName"
          label="工单名称"
          width="150"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="person"
          label="执行人员"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="tel"
          label="联系电话"
          width="150"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="submissionTime"
          label="提交时间"
          width="160"
        >
        <template slot-scope="scope"><span>{{timeTrans(scope.row.submissionTime)}}</span></template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="maintenanceTicketState"
          label="状态"
        >
          <template slot-scope="scope">
            <span>{{
              scope.row.maintenanceTicketState == 0 ? "待派单": scope.row.maintenanceTicketState == 1 ? "待确认": " 已完成"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="operation"
          label="操作"
          width="200"
        >
          <template slot-scope="scope">
            <el-button style="color: #0bb197" type="text" size="small" @click="query(scope.row)"
              >详情</el-button
            >
            <el-button
              v-if="scope.row.maintenanceTicketState == 0"
              style="color: #0bb197"
              type="text"
              size="small"
              @click="transfer(scope.row)"
              >派/转单
            </el-button>
            <el-button
              v-if="scope.row.maintenanceTicketState == 1"
              style="color: #0bb197"
              type="text"
              size="small"
              @click="examine(scope.row)"
              >审核
            </el-button>
            <span></span>
          </template>
        </el-table-column>
      </el-table>

      

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="count"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :background="true"
        class="msg-pagination-container"
      >
      </el-pagination>

      <!-- 新增数据 -->
      <el-dialog title="新增工单" :visible.sync="addDialog">
        <el-form :model="addData" :rules="addUserRules" ref="addData">
          <div class="addB">
            <el-form-item label="工单类型">
              <el-input
                v-model="addData.ticketType"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="工单编号">
              <el-input
                  v-model="addData.ticketNum"
                  autocomplete="off"
                ></el-input>
            </el-form-item>
          </div>
          <div class="addB">
            <el-form-item label="工单名称">
              <el-input v-model="addData.ticketName" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="执行人员">
                <el-input
                  v-model="addData.person"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
          </div>
          <div class="addB">  
              <el-form-item label="联系电话" prop="tel">
                <el-input
                  v-model="addData.tel"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item label="提交时间">
              <!-- <el-input
                v-model="addData.submissionTime"
                autocomplete="off"
              ></el-input> -->
              <el-date-picker
              value-format="timestamp"
              style="width: 350px"
              v-model="addData.submissionTime"
              type="date"
              placeholder="选择时段"
            >
            </el-date-picker>
            </el-form-item>
          </div>
          <div class="addB">
            <el-form-item label="状态">
              <!-- <el-input
                v-model="addData.maintenanceTicketState"
                autocomplete="off"
              ></el-input> -->
               <el-select
              v-model="addData.maintenanceTicketState"
              style="width: 350px"
            >
              <el-option label="每月" :value="0"></el-option>
              <el-option label="每季度" :value="1"></el-option>
            </el-select>
            </el-form-item>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="button" @click="addDialog = false">取 消</el-button>
          <el-button type="primary" @click="add" style="background-color:#0bb197;border:1px solid #0bb197;">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 详情信息 -->
      <el-dialog title="详情信息" :data="queryData" :visible.sync="queryDialog"  style="color:0bb197">
        <div class="queryDetails">
          <p>工单编码：{{row.ticketNum}}</p>
          <p>工单名称：{{row.ticketName}}</p>
        </div>
        <div class="queryDetails">
          <p>工单类型：{{row.ticketType}}</p>
          <p>执行人员：{{row.person}}</p>
        </div>
        <div class="queryDetails">
          <p>联系电话：{{row.tel}}</p>
          <p>提交时间：{{queryData.submissionTime}}</p>
        </div>
        <div class="queryDetails">
          <p>状态：{{row.maintenanceTicketState==0?"待派单":row.maintenanceTicketState==1?"待确认":"已完成"}}</p>
        </div>
        <div  class="maintenance">
          <p>维保情况：</p>
          <span>1号电梯</span>
          <span>2号电梯</span>
          <span>3号电梯</span>
          <span>4号电梯</span>
          <span>5号电梯</span>
          <span>6号电梯</span>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import time from "../utils/time"
export default {
  mixins:[time],
  name:'addData',
  data: function () {
    //验证手机号的规则
    let checkMobile = (rule,value,cb) =>{
      const regMobile = /^1[3-9]\d{9}$/
      if(regMobile.test(value)){
        return cb();
      }
      cb(new Error('请输入正确的手机号'))
    }

    return {
      uintName:"",
      exePerson:"",
      status:"",
      startTime: "",
      endTime:"",
      addDialog: false,
      disDialog:false,
      tableData: [],
      total: 1,
      count: 5,
      page: 1,
      addData: {
        ticketType: "",
        ticketNum: "",
        ticketName: "",
        person: "",
        tel:'',
        submissionTime: "",
        maintenanceTicketState: 0
      },
      addUserRules:{
        tel:[
          {required:true,message:'手机号不能为空'},
          {validator:checkMobile,message:'请输入正确的手机号',trigger:'blur'}
        ]
      },
      row:'',
      queryDialog:false,
      queryData:{},
      transferData:{},
      statusOptions:[{
        value:0,
        label:'待派单'
      },{
        value:1,
        label:'待确认'
      },{
        value:2,
        label:'已完成'
      }],
      statusValue:'',
    };
  },
  methods: {
    //分页
    handleSizeChange(val){
      this.count = val;
      this.getData();
    },
    handleCurrentChange(val){
      this.page = val;
      this.getData();
    },
    //数据渲染
    getData: function () {
      this.$axios({
        url: "/smart/maintenance-ticket/list",
        method: "get",
        params: {
          pageNum: this.page,
          pageSize: this.count,
          uintName:this.uintName,
          exePerson:this.exePerson,
          status:this.status,
          startTime:this.startTime,
          endTime:this.endTime
          },
      }).then(({ data }) => {
        if (data.code == 2000) {
          this.tableData = data.data.list;
          this.total = data.data.total;
        } else {
          this.$message.error("数据加载失败！");
        }
      });
    },
    //
    //查询
    onSearch:function(){
      this.getData();
    },
    //重置
    onReset: function () {
      this.uintName='',
      this.exePerson='',
      this.status='',
      this.startTime='',
      this.endTime='',
      this.getData();
    },
    //新增
    add: function () {
      this.$axios({
        url: "/smart/maintenance-ticket/add",
        method: "post",
        data: this.addData,
      }).then(({ data }) => {
        if ((data.code = 2000)) {
          console.log(data);
          this.$message.success("添加成功");
          this.addDialog = false;
          this.getData();
        } else {
          console.log(data);
          this.$message.error("添加失败");
        }
      });
    },
    //操作派/转单
    transfer:function(row){
      this.$prompt('请选择执行人员', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        console.log(value);
        //赋值
        let transferData = {...row};
        this.transferData = transferData;
        // console.log(transferData);
        this.transferData.person = value; 
        this.transferData.maintenanceTicketState = 1; 
        this.$axios({
          url:"/smart/maintenance-ticket/edit",
          method:"PUT",
          data:this.transferData,
        }).then(({data})=>{
          if (data.code == 2000) {
            this.$message.success("成功");
            this.getData();
          }
        })     
      }).catch(() => {
          this.$message({
            type: 'error',
            message: '取消输入'
          });       
        });
    },
    //审核
    examine:function(row){
      this.$confirm('是否确认通过提交的维保任务, 是否继续?', {
          confirmButtonText: '审核通过',
          cancelButtonText: '审核驳回',
          confirmButtonClass:"buttonzlj",
          cancelButtonClass:"buttonzlp",
        }).then(() => {
          //赋值
          let transferData = {...row};
          this.transferData = transferData;
          // console.log(transferData);
          this.transferData.maintenanceTicketState = 2; 
          this.$axios({
            url:"/smart/maintenance-ticket/edit",
            method:"PUT",
            data:this.transferData,
          }).then(({data})=>{
            if (data.code == 2000) {
              this.$message.success("成功");
              this.getData();
            }
          })     
        }).catch(() => {
          //赋值
          let transferData = {...row};
          this.transferData = transferData;
          this.transferData.maintenanceTicketState = 1; 
          this.$axios({
            url:"/smart/maintenance-ticket/edit",
            method:"PUT",
            data:this.transferData,
          }).then(({data})=>{
            if (data.code == 2000) {
              this.getData();
            }
          })
          this.$message({
            type: 'error',
            message: '已取消',
          });          
        });
    },
    //详情信息
    query:function(row){
      //点击显示弹窗
      this.queryDialog=true;
      //赋值
      this.row = row;
      this.$axios({
        url:"/smart/maintenance-ticket/queryById/" + row.id
      }).then(({data})=>{
        if(data.code==2000){
          this.queryData = data.data;
          this.queryData.submissionTime=this.timeTrans(this.queryData.submissionTime);
        }else{
          this.$message.error(data.msg)
        }
      })
    }
  },
  mounted: function () {
    this.getData();
  },
};
</script>

<style scoped>
.top {
  display: flex;
  justify-content: space-between;
  margin: 20px 20px 10px 25px;
}
.home {
  height: 95%;
}
.header {
  height: 60px;
  background: white;
  margin: 10px 20px 20px;
  padding-top: 18px;
  padding-left: 15px;
  border-radius: 8px;
}
.reset {
  color: rgb(197, 196, 196);
}
.reset:hover {
  color: #0bb197;
}
.el-input,
.el-select {
  margin-right: 10px;
}
.el-button {
  margin-left: 10px;
}
.body {
  background: white;
  margin: 10px 20px;
  height: 84%;
  border-radius: 8px;
}
.div {
  display: flex;
  justify-content: space-between;
  padding: 20px;
}
.el-pagination {
  margin: 25px 10px 0 0;
  display: flex;
  justify-content: right;
}
.el-form-item{
  width: 350px;
}
.addB{
  width: 100%;
  display: flex;
  justify-content: space-evenly;
}
.dialog-footer{
  display: flex;
  justify-content: center;
}
.button:hover{
  color: #0bb197;
  border: 1px solid #0bb197;
}
</style>
<style>
.el-dialog__header,
.msg-pagination-container.is-background .el-pager li:not(.disabled).active {
  /*当前选中页数的样式进行修改*/
  background-color: #0bb197;
  color: #fff;
}
.el-dialog__title,
.el-dialog__headerbtn .el-dialog__close{
  color: white;
}
.queryDetails{
  height: 60px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  margin: 0 15px;
}
.queryDetails p{
  width: 50%;
}
.maintenance{
  margin: 0 15px;
  display: flex;
  text-align: center;
}
.maintenance span{
  width: 70px;
  height: 30px;
  margin: 0 25px;
  background-color: #0bb197;
  color: white;
  line-height: 30px;
}
.el-button--primary {
    color: #FFF;
    background-color:#0bb197;
    border-color: #0bb197;
}
.buttonzlj{
    color: #FFF;
    background-color:#0bb197 !important;
    border-color: #0bb197 !important;
}
.buttonzlp{
    color: #c0c4cc;
    background-color:#FFF;  
}
.buttonzlp:hover{
    color:#0bb197;
    border:1px solid #0bb197 ;
}
</style>
